/*

Make Foreign MNE data/country_data_unique
 */

//Set directories
cd ""  /* PROJECT ROOT FOLDER */
global data "data"



set more off




*******************************************************
*Create dataset for foreign MNLs
*******************************************************

cap program drop clean_formnls
program define clean_formnls
  syntax , year(integer)

  //load affiliate level data 
  use if missing(firmid)==0 using "$data/formnl_us_affiliates`year'.dta", clear
	rename *, lower
  rename usid us_id_in


  **drop if USID is not majority owned 
  tab in_type, miss
  drop if in_typeownership~="Majority"
  
  
 // make foreign country of ownership variable 
   *note: patch here to deal with multiple countries/firm due to multiple bea usids mapping to one census firmid
   *patch uses modal country (where most firms froms) followed by emp
 bys firmid: egen modal_ubo=mode(in_ctryubo)
 sort firmid in_sales in_emp in_ctryubo
 bys firmid: gen temp=in_ctryubo if _n==_N
 bys firmid: egen temp2=mean(temp)
 replace modal_ubo=temp2 if modal_ubo==.
 
  gen countrycode=modal_ubo
 
 merge m:1 countrycode using $data/country_data_unique.dta, keepusing(countrycode iso3)
 rename iso3 for_mnl_country
 label variable for_mnl_country "Modal country for the Foreign MNE"
 tab countrycode if _merge==1
 drop if _merge==2
 drop _merge temp*
**************************************


  //define as manufacturing if parent is manufacturing 
  tostring in_indaff, replace 
  gen n1 = substr(in_indaff,1,1)
  destring n1, replace 
  gen manuf = inrange(in_indparent,30,32) | in_indparent==3

  //sample selection 
  replace in_emp = 0 if missing(in_emp)
  replace in_sales = 0 if missing(in_sales)
  keep if (in_emp>0 | in_sales>0)

  gen in_man_emp = in_emp if (n1==3)
  gen in_man_sales = in_sales if (n1==3)
  gen number_of_usaff = 1
  gen number_of_man_usaff = 1 if (n1==3)

  //collapse to firm level
  collapse (sum) in_sales in_emp in_man_emp in_man_sales number_of_usaff number_of_man_usaff manuf, by(firmid for_mnl_country) 

  label var number_of_usaff "number of us affilaites owned by foreign MNL" 
  label var number_of_man_usaff "number of manufacturing us affilaites owned by foreign MNL" 

  label variable manuf "I think this tells us if the foreign parent or its US affils are in manuf"

  rename manuf for_affil_manuf_status

  save $data/formnl_us_affiliates_firms`year'.dta, replace 

  summ for_affil_manuf_status, d


end //end program


 clean_formnls, year(2007)
 
/*
forvalues yr=1997/2012 {
  clean_formnls, year(`yr')
}
*/


